#include "junix.h"
#include <sys/socket.h>

int main(int argc, char **argv)
{
	char buf[1024];

	int fds[2];
	if( socketpair(AF_UNIX,SOCK_STREAM,0,fds) == -1)
		err_sys("socketpair");

	pid_t pid=fork();
	if(pid<0)
		err_sys("fork");

	if(pid)
	{
		ssize_t sz;
		int fd=open(__FILE__,O_RDONLY);
		if(fd==-1)
			err_sys("open");

		if (close(fds[0])==-1)
			err_sys("close");

		while( (sz=read(fd,buf,1)>0))
			if( write(fds[1],buf,sz)!=sz)
				err_sys("write");

		if(sz<0)
			err_sys("read");

	}else{
		if (close(fds[1])==-1)
			err_sys("close");

		ssize_t sz;
		while( (sz=read(fds[0],buf,1)>0))
			if( write(STDOUT_FILENO,buf,sz)!=sz)
				err_sys("write");

		if(sz<0)
			err_sys("read");

	}

	return 0;
}

